/*********************************************************************
*
* Software License Agreement (BSD License)
*
*  Copyright (c) 2010, Heriot-Watt University, UK.
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Heriot-Watt University nor the names of
*     its contributors may be used to endorse or promote products
*     derived from this software without specific prior written
*     permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*
*  Author: Joel Cartwright
*
*********************************************************************/

#ifndef SERVICEFILTER_H_
#define SERVICEFILTER_H_

#include <boost/foreach.hpp>

#include <matchmaker/service_details.h>

namespace matchmaker
{

class ServiceFilter
{
public:
  /**
   * @brief Filters a service list with a unary inclusion filter
   * @param in input list of services
   * @param includeFn function to control inclusion of services (true = include)
   * @param out output list of services, will be cleared before filtering
   * @return output list
   */
  static L_ServiceDetails * includeIf(const L_ServiceDetails & in,
      boost::function<bool (const ServiceDetailsPtr& service)> includeFn,
      L_ServiceDetails * out)
  {
    assert(out != NULL);
    out->clear();

    BOOST_FOREACH(const ServiceDetailsPtr& service, in) {
      if (includeFn(service)) out->push_back(service);
    }
    return out;
  }

  /**
   * @brief Filters a service list based on a set of IDs to include
   * @param in input list of services
   * @param includeIds set of service IDs to include
   * @param out output list of services, will be cleared before filtering
   * @return output list
   */
  static L_ServiceDetails * includeWithIds(const L_ServiceDetails& in,
      const std::set<std::string> * includeIds,
      L_ServiceDetails * out)
  {
    assert(includeIds != NULL);

    return includeIf(in, boost::bind(&ServiceFilter::testServiceIdMatch, _1, includeIds), out);
  }

  /**
   * @brief Filter that matches services that can be used now
   * @param service details of service to test
   * @return true if service usable now, false otherwise
   */
  static bool testServiceUsable(const ServiceDetailsPtr& service)
  {
    return service->status.isUsable();
  }

  /**
   * @brief Filter that matches services that can be used now, or are busy
   * @param service details of service to test
   * @return true if service usable or busy now, false otherwise
   */
  static bool testServiceUsableOrBusy(const ServiceDetailsPtr& service)
  {
    return service->status.isUsableOrBusy();
  }

  /**
   * @brief Filter that matches services with an ID in the given set
   * @param service object to test
   * @param includeIds service ID inclusion set
   * @return true if service ID is in the inclusion set, false otherwise
   */
  static bool testServiceIdMatch(const ServiceDetailsPtr& service, const std::set<std::string> * includeIds)
  {
    if (includeIds->find(service->id) != includeIds->end()) return true;
    return false;
  }

};

}

#endif /* SERVICEFILTER_H_ */
